﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;
namespace Controller
{
    public class ProductController:ControllerBase
    {
        
        public IList<ProductEntity> FindAllProductEntities()
        {
            IList<Product> products = (from p in Sdc.Products
                                       where p.Visibility == true 
                                       orderby p.No
                                select p).ToList();
            IList<ProductEntity> productEntities = new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }

        public ProductEntity FindProductById(int id) {
            Product product = (from p in Sdc.Products
                              where p.Id==id
                              select p).SingleOrDefault();
            ProductEntity productEntity = TransformProductToProductEntity(product);
            return productEntity;

        }

        public static ProductEntity TransformProductToProductEntity(Product product)
        {
            if(product!=null)
            {
                ProductEntity productEntity = new ProductEntity();
                productEntity.Id = product.Id;
                productEntity.Name = product.Name;
                productEntity.CategoryId = product.CategoryId;
                productEntity.Description = product.Description;
                productEntity.No = product.No;
                productEntity.Remark = product.Remark;
                productEntity.Stock = product.Stock;
                productEntity.Type = product.Type;
                productEntity.CategoryName = product.Category.Name;
                productEntity.Revision = product.Revision;
                productEntity.ProductNoAndRevision = product.No + "/ " + product.Revision;
                productEntity.Visibility = product.Visibility;
                return productEntity;
            }
            else
            {
                return null;
            }

        }

        public static Product TransformProductEntityToProduct(ProductEntity productEntity)
        {
            Product product = new Product();
            product.Id = productEntity.Id;
            product.Name = productEntity.Name;
            product.CategoryId = productEntity.CategoryId;
            product.Description = productEntity.Description;
            product.No = productEntity.No;
            product.Remark = productEntity.Remark;
            product.Stock = productEntity.Stock;
            product.Type = productEntity.Type;
            product.Revision = productEntity.Revision;
            product.Visibility = productEntity.Visibility;
            return product;
        }


        public void updateStock(int productId,int stockInQty)
        {
            Product product = (from p in Sdc.Products
                               where p.Id == productId
                               select p).SingleOrDefault();
            product.Stock = product.Stock + stockInQty;
            Sdc.SubmitChanges();
        }

        public IList<ProductEntity> FindInHandProductByKeyword(string keyword)
        {
            IList<Product> products = (from p in Sdc.Products
                                       where (p.Name.Contains(keyword) || p.No.Contains(keyword)
                                       || p.Description.Contains(keyword)) && p.Stock>0 &&p.Visibility==true
                                       select p).ToList();
            IList<ProductEntity> productEntities = new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }

        public IList<ProductEntity> FindInHandProductByCategoryAndKeword(int categoryId, string keyword)
        {
            IList<Product> products = (from p in Sdc.Products
                                       where p.CategoryId == categoryId
                                             && (p.Name.Contains(keyword) || p.No.Contains(keyword)
                                                 || p.Description.Contains(keyword)) && p.Stock>0 && p.Visibility == true
                                       select p).ToList();
            IList<ProductEntity> productEntities = new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }

        public IList<ProductEntity> FindInHandProductEntitiesByCategory(int categoryId)
        {
            IList<Product> products = (from p in Sdc.Products
                                       where p.CategoryId == categoryId
                                       && p.Stock > 0 && p.Visibility == true
                                       select p).ToList();
            IList<ProductEntity> productEntities = new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }

        public IList<ProductEntity> FindAllInHandProductEntities()
        {
            IList<Product> products = (from p in Sdc.Products
                                       where p.Stock > 0 && p.Visibility == true
                                       select p).ToList();
            IList<ProductEntity> productEntities = new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }
        public IList<ProductEntity> FindProductEntitiesByConditions(int categoryId, string keyword, bool onlyShowProductInHand)
        {
            var products = (from p in Sdc.Products 
                            where p.Visibility == true
                                select p);
            if(categoryId!=0)
            {
                products = (from p in products
                            where p.CategoryId == categoryId
                            select p);
            }

            if(!string.IsNullOrEmpty(keyword))
            {
                products = (from p in products
                            where p.Name.Contains(keyword) || p.No.Contains(keyword) || p.Description.Contains(keyword)
                            select p);
            }

            if(onlyShowProductInHand)
            {
                products = (from p in products
                            where p.Stock > 0
                            select p);
            }
            IList<ProductEntity> productEntities = new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }
        public IList<ProductEntity> FindProductByKeyword(string keyword)
        {
            IList<Product> products = (from p in Sdc.Products
                                       where (p.Name.Contains(keyword) || p.No.Contains(keyword)
                                       || p.Description.Contains(keyword)) && p.Visibility ==true
                                       select p).ToList();
            IList<ProductEntity> productEntities = new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }

        public  IList<ProductEntity> FindProductByCategoryAndKeword(int categoryId, string keyword)
        {
            IList<Product> products = (from p in Sdc.Products
                                       where p.CategoryId == categoryId
                                             && (p.Name.Contains(keyword) || p.No.Contains(keyword)
                                                 || p.Description.Contains(keyword)) && p.Visibility ==true
                                       select p).ToList();
            IList<ProductEntity> productEntities=new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }

        public IList<ProductEntity> FindProductEntitiesByCategory(int categoryId)
        {
            IList<Product> products = (from p in Sdc.Products
                                       where p.CategoryId == categoryId && p.Visibility == true
                                      orderby p.No ascending 
                                       select p).ToList();
            IList<ProductEntity> productEntities = new List<ProductEntity>();
            foreach (var product in products)
            {
                ProductEntity productEntity = TransformProductToProductEntity(product);
                productEntities.Add(productEntity);
            }
            return productEntities;
        }

        public int AddProduct(ProductEntity productEntity)
        {
            Product product = TransformProductEntityToProduct(productEntity);
            product.Stock = 0;
            product.Remark = "";
            Sdc.Products.InsertOnSubmit(product);
            Sdc.SubmitChanges();
            int productId = product.Id;
            return productId;
        }


        public void DeleteProduct(ProductEntity productEntity)
        {

            Product product = (from p in Sdc.Products
                               where p.Id == productEntity.Id
                               select p).SingleOrDefault();
            Sdc.Products.DeleteOnSubmit(product);
           Sdc.SubmitChanges();
         }

        public void updateProduct(ProductEntity updatingProduct)
        {
            Product product = (from p in Sdc.Products
                               where p.Id == updatingProduct.Id
                               select p).SingleOrDefault();
            product.Name = updatingProduct.Name;
            product.No = updatingProduct.No;
            product.Description = updatingProduct.Description;
            product.Revision = updatingProduct.Revision;
            product.Remark = updatingProduct.Remark;
            //product.Type = updatingProduct.Type;
            //product.CategoryId = updatingProduct.CategoryId;
            Sdc.SubmitChanges();
        }

        public IList<YearProductPriceEntity> FindYearProductSalesPrice(int productId)
        {
            DateTime systemNow = System.DateTime.Now;
            TimeSpan timeSpan1=new TimeSpan(systemNow.Ticks);

            IList<CustPOItem> custPoItems = (from c in Sdc.CustPOItems
                                             where c.ProductId==productId
                                             orderby c.RequestDate descending
                                             select c).ToList();
            IList<CustPOItem> custPoItems1=new List<CustPOItem>();
            foreach (CustPOItem custPoItem in custPoItems)
            {
                TimeSpan timeSpan2=new TimeSpan(custPoItem.CustPO.Date.Ticks);
                TimeSpan ts = timeSpan1.Subtract(timeSpan2).Duration();
                if(ts.Days<=365)
                {
                    custPoItems1.Add(custPoItem);
                }
            }
            IList<YearProductPriceEntity> yearProductPriceEntities=new List<YearProductPriceEntity>();
            foreach (CustPOItem custPoItem in custPoItems1)
            {
                YearProductPriceEntity yearProductPriceEntity = TransformCustPOItemToYearProductPriceEntity(custPoItem);

                yearProductPriceEntities.Add(yearProductPriceEntity);
            }
            return yearProductPriceEntities;
        }

        public IList<YearProductPriceEntity> FindYearProductPurchasePrice(int productId)
        {
            DateTime systemNow = System.DateTime.Now;
            TimeSpan timeSpan1 = new TimeSpan(systemNow.Ticks);
            IList<VendPOItem> vendorPOItems = (from c in Sdc.VendPOItems
                                             where c.ProductId == productId
                                             orderby c.RequestDate descending
                                             select c).ToList();
            IList<VendPOItem> vendorPOItems1 = new List<VendPOItem>();
            foreach (VendPOItem vendPoItem in vendorPOItems)
            {
                TimeSpan timeSpan2=new TimeSpan(vendPoItem.RequestDate.Ticks);
                TimeSpan ts = timeSpan1.Subtract(timeSpan2).Duration();
                if(ts.Days<=365)
                {
                    vendorPOItems1.Add(vendPoItem);
                }
            }
            IList<YearProductPriceEntity> yearProductPriceEntities = new List<YearProductPriceEntity>();
            foreach (VendPOItem vendorPOItem in vendorPOItems1)
            {
                YearProductPriceEntity yearProductPriceEntity = TransformVendorPOItemToYearProductPriceEntity(vendorPOItem);

                yearProductPriceEntities.Add(yearProductPriceEntity);
            }
            return yearProductPriceEntities;
        }
        public static YearProductPriceEntity TransformCustPOItemToYearProductPriceEntity(CustPOItem custPoItem)
        {
            YearProductPriceEntity yearProductPriceEntity=new YearProductPriceEntity();
            yearProductPriceEntity.ProductId = custPoItem.ProductId;
            yearProductPriceEntity.Date = custPoItem.RequestDate;
            yearProductPriceEntity.VendorOrCustomer = custPoItem.CustPO.Customer.ShortName;
            yearProductPriceEntity.Qty = custPoItem.RequestQty;
            yearProductPriceEntity.Price = custPoItem.Price;
            return yearProductPriceEntity;
        }
        public static YearProductPriceEntity TransformVendorPOItemToYearProductPriceEntity(VendPOItem vendorPoItem)
        {
            YearProductPriceEntity yearProductPriceEntity = new YearProductPriceEntity();
            yearProductPriceEntity.ProductId = vendorPoItem.ProductId;
            yearProductPriceEntity.Date = vendorPoItem.RequestDate;
            yearProductPriceEntity.VendorOrCustomer = vendorPoItem.VendPO.Vendor.ShortName;
            yearProductPriceEntity.Qty = vendorPoItem.RequestQty;
            yearProductPriceEntity.Price = vendorPoItem.Price;
            return yearProductPriceEntity;
        }

        public ProductEntity FindProductByProductNo(string productNo)
        {
            Product product = (from p in Sdc.Products
                               where p.No == productNo
                               select p).SingleOrDefault();
            ProductEntity productEntity = TransformProductToProductEntity(product);
            return productEntity;
        }
    }
}
